package com.cogent.business.helper;

import java.util.HashMap;
import java.util.List;

import com.cogent.common.date.JodaDateUtil;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.dynamicform.facade.IDynamicFormFacade;
import com.cogent.dynamicform.facade.impl.DynamicFormFacadeImpl;

public class DynamicFormHelper {
	public final static String DYNTABLE_FORM_KEY = "DYNTABLE_FORM_LIST";
	public final static String DYNTABLE_FORM_VIEW_KEY = "DYNTABLE_FORM_VIEW_LIST";
	public final static String QUERY_DYNTABLE_FORM_KEY = "QUERY_DYNTABLE_FORM_KEY";
	private final static IDynamicFormFacade dynamicFormFacade = new DynamicFormFacadeImpl();
	
	public static FormDomain getFormDomainByID(String formid) {
		return dynamicFormFacade.getForm(formid);
	}
	
	public static String getScript(FormDomain formDomain) {
		return dynamicFormFacade.getScript(formDomain);
	}
	
	public static String getDynTableInput(FormDomain formDomain, HashMap formMap) {
		return dynamicFormFacade.genDynTableInput(formDomain, formDomain.getDynTableList(), formMap);
	}
	
	public static String getDynTableView(FormDomain formDomain, HashMap datamap) {
		return dynamicFormFacade.genDynTableView(formDomain, formDomain.getDynTableList(), datamap);
	}
	
	public static String getDynTableView(List formColumnDomainList, List viewVlaueList) {
		return dynamicFormFacade.getDynTableView(formColumnDomainList, viewVlaueList);
	}
	
	public static String getWorkFlowInput(FormDomain formDomain, HashMap formMap) {
		return "";
	}
	
	public static String getWorkFlowView(FormDomain formDomain, HashMap formMap) {
		return "";
	}
	
	private static String getLinkAddTap(FormDomain formDomain, String thisid, String referer, HashMap datamap, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		if(datamap.size() < 1)
			datamap = formMap;
		if(MapUtil.isEmpty(formMap, "id")) {
			if(MapUtil.isEmpty(datamap, "id")) {
				String[] linkids = formDomain.getLinkid().split("~");
				formMap.put("id", MapUtil.getStringValue(datamap, linkids[0]));
			} else		
				formMap.put("id", MapUtil.getStringValue(datamap, "id"));
		}
		String[] temps = formDomain.getChecklinktaps().split("\\|");
		String[] checktaps = temps[0].split(";");
		String[] checkvalues = temps[1].split("~");
		String curtempvalue = MapUtil.getStringValue(datamap, checkvalues[0]);
		if(StringUtil.isEmpty(curtempvalue))
			curtempvalue = MapUtil.getStringValue(formMap, checkvalues[0]);
		int curvale = StringUtil.toInt(curtempvalue, 0);
		int stepvalue = StringUtil.toInt(checkvalues[1]);
		if(formDomain.isIsworkflow() && (StringUtil.isEmpty(formDomain.getParenttaps())))
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
		if(StringUtil.isEmpty(formDomain.getLinktaps()))
			formDomain = getFormDomainByID(formDomain.getParenttaps());
		if(!StringUtil.isEmpty(formDomain.getLinktaps())) {
			String onclick = "warning()";
			for(int i=0; i<checktaps.length; i++) {
				String[] checktemps = checktaps[i].split("~");
				int cursteptempvalue = StringUtil.toInt(checktemps[1], 0);
				if((cursteptempvalue > curvale) && (stepvalue != cursteptempvalue))
					break;
				FormDomain curformdomain = getFormDomainByID(checktemps[0]);
				if(checktemps[0].equals(thisid))
					str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(curformdomain.getCn()).append("</a>");
				else if(MapUtil.isEmpty(formMap, "id")) {
					str.append("<a href=\"javascript:void(0)\" onclick=\"").append(onclick).append("\">").append(curformdomain.getCn()).append("</a>");
				} else {
					String linkurl = getOperatorLink(curformdomain) + "&" + checkvalues[0] + "=" + curtempvalue;
					String[] linktaps = formDomain.getLinktaps().split(";");
					String[] curtaps = null;
					for(int j=0; j<linktaps.length; j++) {
						curtaps = linktaps[j].split("\\|");
						if(curtaps[0].equals(curformdomain.getId()))
							break;
						else
							curtaps = null;
					}
					if(curtaps == null)
						linkurl += "&id="  + MapUtil.getStringValue(formMap, "id");
					else if(curtaps.length>1) {
						String[] curtemps = curtaps[1].split(",");
						for(int j=0; j<curtemps.length; j++) {
							String[] temps2 = curtemps[j].split("~");
							String curfield = "id";
							if(temps2.length > 1)
								curfield = temps2[1];
							if(MapUtil.isEmpty(datamap, curfield))
								linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(formMap, curfield);
							else
								linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(datamap, curfield);
						}
					}
					linkurl += "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(curformdomain.getCn()).append("</a>");
				}
					
			}
		}
		return str.toString();
	}
	
	public static String getWorkFlowTap(FormDomain formDomain, String referer, HashMap datamap, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		String type = "mod";
		if(MapUtil.getIntValue(formMap, "workflow_parent_type") == 1)
			type = "view";
		if(datamap.size() < 1)
			datamap = formMap;
		if(MapUtil.isEmpty(datamap, "id")) {
			String[] linkids = formDomain.getLinkid().split("~");
			formMap.put("id", MapUtil.getStringValue(datamap, linkids[0]));
		} else			
			formMap.put("id", MapUtil.getStringValue(datamap, "id"));
		String thisid = formDomain.getId();
		formDomain = getFormDomainByID(MapUtil.getStringValue(formMap, "workflow_parent_typecode"));
		String[] temps = formDomain.getChecklinktaps().split("\\|");
		String[] checktaps = temps[0].split(";");
		String[] checkvalues = temps[1].split("~");
		String curtempvalue = MapUtil.getStringValue(datamap, checkvalues[0]);
		if(StringUtil.isEmpty(curtempvalue))
			curtempvalue = MapUtil.getStringValue(formMap, checkvalues[0]);
		int curvale = StringUtil.toInt(curtempvalue, 0);
		for(int i=0; i<checktaps.length; i++) {
			String[] checktemps = checktaps[i].split("~");
			int cursteptempvalue = StringUtil.toInt(checktemps[1], 0);
			FormDomain curformdomain = getFormDomainByID(checktemps[0]);
			if(checktemps[0].equals(thisid))
				str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(curformdomain.getCn()).append("</a>");
			else {
				String linkurl = "";
				if((cursteptempvalue == 1) && (curvale > 0) && (curformdomain.getId().equals("WF_RECORD")))
					linkurl = "DynamicForm_workflowlist.action?formid=WF_RECORD&wfid=" + MapUtil.getStringValue(datamap, "id") 
							+ "&workflow_parent_typecode=" + formDomain.getId() + "&workflow_parent_typeid=" + MapUtil.getStringValue(datamap, "id")
							+ "&workflow_parent_type=" + MapUtil.getIntValue(formMap, "workflow_parent_type", 0);
				else
					linkurl = "DynamicForm_" + type + ".action?formid=" + curformdomain.getId();
				linkurl += "&" + checkvalues[0] + "=" + curtempvalue;
				String[] linktaps = formDomain.getLinktaps().split(";");
				String[] curtaps = null;
				for(int j=0; j<linktaps.length; j++) {
					curtaps = linktaps[j].split("\\|");
					if(curtaps[0].equals(curformdomain.getId()))
						break;
					else
						curtaps = null;
				}
				if(curtaps == null)
					linkurl += "&id="  + MapUtil.getStringValue(formMap, "id");
				else if(curtaps.length>1) {
					String[] curtemps = curtaps[1].split(",");
					for(int j=0; j<curtemps.length; j++) {
						String[] temps2 = curtemps[j].split("~");
						String curfield = "id";
						if(temps2.length > 1)
							curfield = temps2[1];
						linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(datamap, curfield);
					}
				}
				linkurl += "&referer=" + referer;
				str.append("<a href=\"").append(linkurl).append("\">").append(curformdomain.getCn()).append("</a>");
			}
		}
		
		return str.toString();
	}
	
	public static String getAddTap(FormDomain formDomain, String referer, HashMap datamap, HashMap formMap) {
		if(!MapUtil.isEmpty(formMap, "workflow_parent_type"))
			return getWorkFlowTap(formDomain, referer, datamap, formMap);
		if((formDomain.isIsworkflow()) && (MapUtil.getIntValue(datamap, "status") > 0)) {
			formMap.put("workflow_parent_type", "0");
			formMap.put("workflow_parent_typecode", formDomain.getId());
			formMap.put("workflow_parent_typeid", MapUtil.getStringValue(datamap, "id"));
			return getWorkFlowTap(formDomain, referer, datamap, formMap);
		}
		if(!StringUtil.isEmpty(formDomain.getChecklinktaps()))
			return getLinkAddTap(formDomain, formDomain.getId(), referer, datamap, formMap);
		StringBuffer str = new StringBuffer();
		if(!StringUtil.isEmpty(formDomain.getLinktaps())) {
			String onclick = "warning()";
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
			String[] linktaps = formDomain.getLinktaps().split(";");
			if(MapUtil.isEmpty(datamap, "id")) {
				for(int i=0; i<linktaps.length; i++) {
					String[] curtaps = linktaps[i].split("\\|");
					String tapid = curtaps[0];
					if(curtaps.length > 2)
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
					FormDomain parentform = getFormDomainByID(tapid);
					String name = parentform.getCn();
					if(StringUtil.isEmpty(name))
						name = curtaps[3];
					str.append("<a href=\"javascript:void(0)\" onclick=\"").append(onclick).append("\">")
						.append(name).append("</a>");
				}
			} else {
				for(int i=0; i<linktaps.length; i++) {
					String[] curtaps = linktaps[i].split("\\|");
					String tapid = curtaps[0];
					if(curtaps.length > 2)
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
					FormDomain parentform = getFormDomainByID(tapid);
					String linkurl = getOperatorLink(parentform);
					if(curtaps.length>1) {
						String[] temps = curtaps[1].split(",");
						for(int j=0; j<temps.length; j++) {
							String[] temps2 = temps[j].split("~");
							String curfield = "id";
							if(temps2.length > 1)
								curfield = temps2[1];
							linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(datamap, curfield);
						}
					}
					linkurl += "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(parentform.getCn()).append("</a>");
				}
			}
		}else if(!StringUtil.isEmpty(formDomain.getParenttaps())) {
			FormDomain parentform = getFormDomainByID(formDomain.getParenttaps());
			String id = MapUtil.getStringValue(datamap, formDomain.getLinkid());
			if(StringUtil.isEmpty(id))
				id = MapUtil.getStringValue(formMap, formDomain.getLinkid());
			str.append("<a href=\"DynamicForm_mod.action?formid=").append(parentform.getId()).append("&id=")
				.append(id).append("&referer=").append(referer)
				.append("\">").append(parentform.getCn()).append("</a>");
			String[] linktaps = parentform.getLinktaps().split(";");
			for(int i=0; i<linktaps.length; i++) {
				String[] curtaps = linktaps[i].split("\\|");
				String tapid = curtaps[0];
				if(curtaps.length > 2) {
					if(MapUtil.isEmpty(datamap, curtaps[2]))
						tapid += MapUtil.getStringValue(formMap, curtaps[2]);
					else
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
				}
				if(tapid.equals(formDomain.getId()))
					str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
				else {
					FormDomain curformdomain = getFormDomainByID(tapid);
					String linkurl = getOperatorLink(curformdomain);
					String curLinkid = MapUtil.getStringValue(datamap, formDomain.getLinkid());
					if(StringUtil.isEmpty(curLinkid))
						curLinkid = MapUtil.getStringValue(formMap, formDomain.getLinkid());
					linkurl += "&" + curformdomain.getLinkid() + "=" + curLinkid + "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(parentform.getCn()).append("</a>");
				}
			}
		} else
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
		
		return str.toString();
	}
	
	public static String getOpenerAddTap(FormDomain formDomain, String referer, HashMap datamap, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		if(!StringUtil.isEmpty(formDomain.getOpenerlinktaps())) {
			String onclick = "warning()";
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
			String[] linktaps = formDomain.getOpenerlinktaps().split(";");
			if(MapUtil.isEmpty(datamap, "id")) {
				for(int i=0; i<linktaps.length; i++) {
					String[] curtaps = linktaps[i].split("\\|");
					String tapid = curtaps[0];
					if(curtaps.length > 2)
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
					FormDomain parentform = getFormDomainByID(tapid);
					String name = parentform.getCn();
					if(StringUtil.isEmpty(name))
						name = curtaps[3];
					str.append("<a href=\"javascript:void(0)\" onclick=\"").append(onclick).append("\">")
						.append(name).append("</a>");
				}
			} else {
				for(int i=0; i<linktaps.length; i++) {
					String[] curtaps = linktaps[i].split("\\|");
					String tapid = curtaps[0];
					if(curtaps.length > 2)
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
					FormDomain parentform = getFormDomainByID(tapid);
					String linkurl = getOperatorLink(parentform);
					if(curtaps.length>1) {
						String[] temps = curtaps[1].split(",");
						for(int j=0; j<temps.length; j++) {
							String[] temps2 = temps[j].split("~");
							String curfield = "id";
							if(temps2.length > 1)
								curfield = temps2[1];
							linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(datamap, curfield);
						}
					}
					linkurl += "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(parentform.getCn()).append("</a>");
				}
			}
		}else if(!StringUtil.isEmpty(formDomain.getParenttaps())) {
			FormDomain parentform = getFormDomainByID(formDomain.getParenttaps());
			String id = MapUtil.getStringValue(datamap, formDomain.getLinkid());
			if(StringUtil.isEmpty(id))
				id = MapUtil.getStringValue(formMap, formDomain.getLinkid());
			str.append("<a href=\"DynamicForm_openermod.action?formid=").append(parentform.getId()).append("&id=")
				.append(id).append("&referer=").append(referer)
				.append("\">").append(parentform.getCn()).append("</a>");
			String[] linktaps = parentform.getOpenerlinktaps().split(";");
			for(int i=0; i<linktaps.length; i++) {
				String[] curtaps = linktaps[i].split("\\|");
				String tapid = curtaps[0];
				if(curtaps.length > 2) {
					if(MapUtil.isEmpty(datamap, curtaps[2]))
						tapid += MapUtil.getStringValue(formMap, curtaps[2]);
					else
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
				}
				if(tapid.equals(formDomain.getId()))
					str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
				else {
					FormDomain curformdomain = getFormDomainByID(tapid);
					String linkurl = getOperatorLink(curformdomain);
					String curLinkid = MapUtil.getStringValue(datamap, formDomain.getLinkid());
					if(StringUtil.isEmpty(curLinkid))
						curLinkid = MapUtil.getStringValue(formMap, formDomain.getLinkid());
					linkurl += "&" + curformdomain.getLinkid() + "=" + curLinkid + "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(parentform.getCn()).append("</a>");
				}
			}
		} else
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
		
		return str.toString();
	}
	
	private static String getOperatorLink(FormDomain parentform) {
		if(!StringUtil.isEmpty(parentform.getUrlpath()))
			return parentform.getUrlpath();
		if(parentform.getLinkmod().equals("MOD"))
			return "DynamicForm_mod.action?formid=" + parentform.getId();
		else if(parentform.getLinkmod().equals("OPENERMOD"))
			return "DynamicForm_openermod.action?formid=" + parentform.getId();
		else
			return "DynamicForm_openerlist.action?formid=" + parentform.getId();
	}
	
	public static String getViewTap(FormDomain formDomain, String referer, HashMap datamap, HashMap formMap) {
		if(formDomain.isIsworkflow())
			return getLinkWorkFlowViewTap(formDomain, formDomain.getId(), referer, datamap, formMap);
		if(!StringUtil.isEmpty(formDomain.getChecklinktaps()))
			return getLinkViewTap(formDomain, formDomain.getId(), referer, datamap, formMap);
		StringBuffer str = new StringBuffer();
		if(!StringUtil.isEmpty(formDomain.getLinktaps())) {
			String onclick = "warning()";
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
			String[] linktaps = formDomain.getLinktaps().split(";");
			if(MapUtil.isEmpty(datamap, "id")) {
				for(int i=0; i<linktaps.length; i++) {
					String[] curtaps = linktaps[i].split("\\|");
					String tapid = curtaps[0];
					if(curtaps.length > 2)
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
					FormDomain parentform = getFormDomainByID(tapid);
					String name = parentform.getCn();
					if(StringUtil.isEmpty(name))
						name = curtaps[3];
					str.append("<a href=\"javascript:void(0)\" onclick=\"").append(onclick).append("\">")
						.append(name).append("</a>");
				}
			} else {
				for(int i=0; i<linktaps.length; i++) {
					String[] curtaps = linktaps[i].split("\\|");
					String tapid = curtaps[0];
					if(curtaps.length > 2)
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
					FormDomain parentform = getFormDomainByID(tapid);
					String linkurl = getViewLink(parentform);
					if(curtaps.length>1) {
						String[] temps = curtaps[1].split(",");
						for(int j=0; j<temps.length; j++) {
							String[] temps2 = temps[j].split("~");
							String curfield = "id";
							if(temps2.length > 1)
								curfield = temps2[1];
							linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(datamap, curfield);
						}
					}
					linkurl += "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(parentform.getCn()).append("</a>");
				}
			}
		}else if(!StringUtil.isEmpty(formDomain.getParenttaps())) {
			FormDomain parentform = getFormDomainByID(formDomain.getParenttaps());
			String id = MapUtil.getStringValue(datamap, formDomain.getLinkid());
			if(StringUtil.isEmpty(id))
				id = MapUtil.getStringValue(formMap, formDomain.getLinkid());
			str.append("<a href=\"DynamicForm_view.action?formid=").append(parentform.getId()).append("&id=")
				.append(id).append("&referer=").append(referer)
				.append("\">").append(parentform.getCn()).append("</a>");
			String[] linktaps = parentform.getLinktaps().split(";");
			for(int i=0; i<linktaps.length; i++) {
				String[] curtaps = linktaps[i].split("\\|");
				String tapid = curtaps[0];
				if(curtaps.length > 2) {
					if(MapUtil.isEmpty(datamap, curtaps[2]))
						tapid += MapUtil.getStringValue(formMap, curtaps[2]);
					else
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
				}
				if(tapid.equals(formDomain.getId()))
					str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
				else {
					FormDomain curformdomain = getFormDomainByID(tapid);
					String linkurl = getViewLink(curformdomain);
					String curLinkid = MapUtil.getStringValue(datamap, formDomain.getLinkid());
					if(StringUtil.isEmpty(curLinkid))
						curLinkid = MapUtil.getStringValue(formMap, formDomain.getLinkid());
					linkurl += "&" + curformdomain.getLinkid() + "=" + curLinkid + "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(parentform.getCn()).append("</a>");
				}
			}
		} else
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
		
		return str.toString();
	}
	
	private static String getLinkViewTap(FormDomain formDomain, String thisid, String referer, HashMap datamap, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		if(datamap.size() < 1)
			datamap = formMap;
		if(MapUtil.isEmpty(datamap, "id")) {
			String[] linkids = formDomain.getLinkid().split("~");
			formMap.put("id", MapUtil.getStringValue(datamap, linkids[0]));
		} else			
			formMap.put("id", MapUtil.getStringValue(datamap, "id"));
		String[] temps = formDomain.getChecklinktaps().split("\\|");
		String[] checktaps = temps[0].split(";");
		String[] checkvalues = temps[1].split("~");
		String curtempvalue = MapUtil.getStringValue(datamap, checkvalues[0]);
		if(StringUtil.isEmpty(curtempvalue))
			curtempvalue = MapUtil.getStringValue(formMap, checkvalues[0]);
		int curvale = StringUtil.toInt(curtempvalue, 0);
		if(StringUtil.isEmpty(formDomain.getParenttaps()))
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
		if(StringUtil.isEmpty(formDomain.getLinktaps()))
			formDomain = getFormDomainByID(formDomain.getParenttaps());
		if(!StringUtil.isEmpty(formDomain.getLinktaps())) {
			String onclick = "warning()";
			for(int i=0; i<checktaps.length; i++) {
				String[] checktemps = checktaps[i].split("~");
				int cursteptempvalue = StringUtil.toInt(checktemps[1], 0);
				if(curvale != 0)
					cursteptempvalue = cursteptempvalue + 1;
				if(cursteptempvalue > curvale)
					break;
				FormDomain curformdomain = getFormDomainByID(checktemps[0]);
				if(checktemps[0].equals(thisid))
					str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(curformdomain.getCn()).append("</a>");
				else if(MapUtil.isEmpty(formMap, "id")) {
					str.append("<a href=\"javascript:void(0)\" onclick=\"").append(onclick).append("\">").append(curformdomain.getCn()).append("</a>");
				} else {
					String linkurl = getViewLink(curformdomain) + "&" + checkvalues[0] + "=" + curtempvalue;
					String[] linktaps = formDomain.getLinktaps().split(";");
					String[] curtaps = null;
					for(int j=0; j<linktaps.length; j++) {
						curtaps = linktaps[j].split("\\|");
						if(curtaps[0].equals(curformdomain.getId()))
							break;
						else
							curtaps = null;
					}
					if(curtaps == null)
						linkurl += "&id="  + MapUtil.getStringValue(formMap, "id");
					else if(curtaps.length>1) {
						String[] curtemps = curtaps[1].split(",");
						for(int j=0; j<curtemps.length; j++) {
							String[] temps2 = curtemps[j].split("~");
							String curfield = "id";
							if(temps2.length > 1)
								curfield = temps2[1];
							linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(datamap, curfield);
						}
					}
					linkurl += "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(curformdomain.getCn()).append("</a>");
				}
					
			}
		}
		return str.toString();
	}
	
	private static String getLinkWorkFlowViewTap(FormDomain formDomain, String thisid, String referer, HashMap datamap, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		if(datamap.size() < 1)
			datamap = formMap;
		if(MapUtil.isEmpty(datamap, "id")) {
			String[] linkids = formDomain.getLinkid().split("~");
			formMap.put("id", MapUtil.getStringValue(datamap, linkids[0]));
		} else			
			formMap.put("id", MapUtil.getStringValue(datamap, "id"));
		String[] temps = formDomain.getChecklinktaps().split("\\|");
		String[] checktaps = temps[0].split(";");
		String[] checkvalues = temps[1].split("~");
		String curtempvalue = MapUtil.getStringValue(datamap, checkvalues[0]);
		if(StringUtil.isEmpty(curtempvalue))
			curtempvalue = MapUtil.getStringValue(formMap, checkvalues[0]);
		int curvale = StringUtil.toInt(curtempvalue, 0);
		for(int i=0; i<checktaps.length; i++) {
			String[] checktemps = checktaps[i].split("~");
			int cursteptempvalue = StringUtil.toInt(checktemps[1], 0);
			FormDomain curformdomain = getFormDomainByID(checktemps[0]);
			if(checktemps[0].equals(thisid))
				str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(curformdomain.getCn()).append("</a>");
			else {
				String linkurl = "";
				if(curvale < cursteptempvalue)
					continue;
				if((cursteptempvalue == 1) && (curvale > 0) && (curformdomain.getId().equals("WF_RECORD")))
					linkurl = "DynamicForm_workflowlist.action?formid=WF_RECORD&wfid=" + MapUtil.getStringValue(datamap, "id") 
							+ "&workflow_parent_typecode=" + formDomain.getId() + "&workflow_parent_typeid=" + MapUtil.getStringValue(datamap, "id")
							+ "&workflow_parent_type=1";
				else
					linkurl = getViewLink(curformdomain);
				linkurl += "&" + checkvalues[0] + "=" + curtempvalue;
				String[] linktaps = formDomain.getLinktaps().split(";");
				String[] curtaps = null;
				for(int j=0; j<linktaps.length; j++) {
					curtaps = linktaps[j].split("\\|");
					if(curtaps[0].equals(curformdomain.getId()))
						break;
					else
						curtaps = null;
				}
				if(curtaps == null)
					linkurl += "&id="  + MapUtil.getStringValue(formMap, "id");
				else if(curtaps.length>1) {
					String[] curtemps = curtaps[1].split(",");
					for(int j=0; j<curtemps.length; j++) {
						String[] temps2 = curtemps[j].split("~");
						String curfield = "id";
						if(temps2.length > 1)
							curfield = temps2[1];
						linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(datamap, curfield);
					}
				}
				linkurl += "&referer=" + referer;
				str.append("<a href=\"").append(linkurl).append("\">").append(curformdomain.getCn()).append("</a>");
			}
		}
		
		return str.toString();
	}
	
	public static String getOpenerViewTap(FormDomain formDomain, String referer, HashMap datamap, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		if(!StringUtil.isEmpty(formDomain.getOpenerlinktaps())) {
			String onclick = "warning()";
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
			String[] linktaps = formDomain.getOpenerlinktaps().split(";");
			if(MapUtil.isEmpty(datamap, "id")) {
				for(int i=0; i<linktaps.length; i++) {
					String[] curtaps = linktaps[i].split("\\|");
					String tapid = curtaps[0];
					if(curtaps.length > 2)
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
					FormDomain parentform = getFormDomainByID(tapid);
					String name = parentform.getCn();
					if(StringUtil.isEmpty(name))
						name = curtaps[3];
					str.append("<a href=\"javascript:void(0)\" onclick=\"").append(onclick).append("\">")
						.append(name).append("</a>");
				}
			} else {
				for(int i=0; i<linktaps.length; i++) {
					String[] curtaps = linktaps[i].split("\\|");
					String tapid = curtaps[0];
					if(curtaps.length > 2)
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
					FormDomain parentform = getFormDomainByID(tapid);
					String linkurl = getViewLink(parentform);
					if(curtaps.length>1) {
						String[] temps = curtaps[1].split(",");
						for(int j=0; j<temps.length; j++) {
							String[] temps2 = temps[j].split("~");
							String curfield = "id";
							if(temps2.length > 1)
								curfield = temps2[1];
							linkurl += "&" + temps2[0] + "=" + MapUtil.getStringValue(datamap, curfield);
						}
					}
					linkurl += "&referer=" + referer;
					str.append("<a href=\"").append(linkurl).append("\">").append(parentform.getCn()).append("</a>");
				}
			}
		}else if(!StringUtil.isEmpty(formDomain.getParenttaps())) {
			FormDomain parentform = getFormDomainByID(formDomain.getParenttaps());
			String id = MapUtil.getStringValue(datamap, formDomain.getLinkid());
			if(StringUtil.isEmpty(id))
				id = MapUtil.getStringValue(formMap, formDomain.getLinkid());
			str.append("<a href=\"DynamicForm_openerview.action?formid=").append(parentform.getId()).append("&id=")
				.append(id).append("&referer=").append(referer)
				.append("\">").append(parentform.getCn()).append("</a>");
			String[] linktaps = parentform.getOpenerlinktaps().split(";");
			for(int i=0; i<linktaps.length; i++) {
				String[] curtaps = linktaps[i].split("\\|");
				String tapid = curtaps[0];
				if(curtaps.length > 2) {
					if(MapUtil.isEmpty(datamap, curtaps[2]))
						tapid += MapUtil.getStringValue(formMap, curtaps[2]);
					else
						tapid += MapUtil.getStringValue(datamap, curtaps[2]);
				}
				if(tapid.equals(formDomain.getId()))
					str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
				else {
					FormDomain curformdomain = getFormDomainByID(tapid);
					String linkurl = getViewLink(curformdomain);
					String curLinkid = MapUtil.getStringValue(datamap, formDomain.getLinkid());
					if(StringUtil.isEmpty(curLinkid))
						curLinkid = MapUtil.getStringValue(formMap, formDomain.getLinkid());
					linkurl += "&" + curformdomain.getLinkid() + "=" + curLinkid;
					str.append("<a href=\"").append(linkurl).append("\">").append(parentform.getCn()).append("</a>");
				}
			}
		} else
			str.append("<a href=\"javascript:void(0)\" class=\"select\">").append(formDomain.getCn()).append("</a>");
		
		return str.toString();
	}
	
	private static String getViewLink(FormDomain parentform) {
		if(parentform.getLinkmod().equals("MOD"))
			return "DynamicForm_view.action?formid=" + parentform.getId();
		else if(parentform.getLinkmod().equals("OPENERMOD"))
			return "DynamicForm_openerview.action?formid=" + parentform.getId();
		else
			return "DynamicForm_viewlist.action?formid=" + parentform.getId();
	}
	
	public static String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		return dynamicFormFacade.genQueryField(formColumnDomain, formMap);
	}
	
	public static String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		return dynamicFormFacade.genInputField(formColumnDomain, formMap);
	}
	
	public static String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		return dynamicFormFacade.getShowValue(formColumnDomain, formMap);
	}
	
	public static String getSequence(BussinessDaoFacade bussinessFacade, String seqname, String prefix) {
		String sql = "select " + seqname + ".Nextval as curval from dual";
		HashMap datamap = bussinessFacade.getOneRow(sql);
		String curvalue = MapUtil.getStringValue(datamap, "curval");
		StringBuffer str = new StringBuffer();
		str.append(prefix);
		for(int i=curvalue.length(); i<12; i++)
			str.append("0");
		str.append(curvalue);
		
		return str.toString();
	}
	
	public static String getSeqNO(BussinessDaoFacade bussinessFacade, String seqname, String prefix) {
		String sql = "select " + seqname + ".Nextval as curval from dual";
		HashMap datamap = bussinessFacade.getOneRow(sql);
		String curshortdate = JodaDateUtil.getCurDate("yyyyMMdd");
		int curvalue = MapUtil.getIntValue(datamap, "curval");
		sql = "select * from sys_recordid where busid='" + seqname + "' and busdate='" + curshortdate + "'";
		HashMap curqueymap = QuerySelectHelper.getChacheValueByID(sql);
		int curseqvalue = 0;
		if(curqueymap.size() > 0)
			curseqvalue = curvalue - MapUtil.getIntValue(curqueymap, "curvalue");
		else {
			curqueymap.put("busid", seqname);
			curqueymap.put("busdate", curshortdate);
			curqueymap.put("curvalue", "" + (curvalue-1));
			curseqvalue = 1;
			bussinessFacade.save("sys_recordid", curqueymap);
			QuerySelectHelper.refreshCacheByID(sql, curqueymap);
		}
		StringBuffer str = new StringBuffer();
		str.append(prefix).append(curshortdate);
		String temp = "" + curseqvalue;
		for(int i=temp.length(); i<7; i++)
			str.append("0");
		str.append(curseqvalue);
		
		return str.toString();
	}
	
	public static boolean isNoViewModel(int num, int type) {
		if(num == 0)
			return false;
		boolean flag = true;
		if((type & num) == type)
			flag = false;
		
		return flag;
	}
	
	public static boolean isNoOptModel(int num, int type) {
		if(num == 0)
			return true;
		boolean flag = false;
		if((type & num) == type)
			flag = true;
		return flag;
	}
	
	public static String genQueryDynTable(FormDomain formDomain, HashMap formMap) {
		return dynamicFormFacade.genQueryDynTable(formDomain, formDomain.getQueryDynTableDomain().getDynTableColumnList(), formMap);
	}
	
	public static String genQueryDynTableView(FormDomain formDomain, HashMap datamap) {
		return dynamicFormFacade.genQueryDynTableView(formDomain, datamap);
	}
	
	public static String genInputField(String type, String name, String showclass, HashMap formMap) {
		return genInputField(type, name, showclass, "", formMap);
	}
	
	public static String genInputField(String type, String name, String showclass, String js, HashMap formMap) {
		FormColumnDomain formcolumndomain = new FormColumnDomain();
		formcolumndomain.setType(type);
		formcolumndomain.setName(name);
		formcolumndomain.setShowclass(showclass);
		formcolumndomain.setJs(js);
		
		return dynamicFormFacade.genInputField(formcolumndomain, formMap);
	}
	
	public static String getShowValue(String type, String name, HashMap formMap) {
		FormColumnDomain formcolumndomain = new FormColumnDomain();
		formcolumndomain.setType(type);
		formcolumndomain.setName(name);
		
		return dynamicFormFacade.getShowValue(formcolumndomain, formMap);
	}
	
	public static String genQueryField(String type, String name, String typecode, HashMap formMap) {
		FormColumnDomain formcolumndomain = new FormColumnDomain();
		formcolumndomain.setType(type);
		formcolumndomain.setTypecode(typecode);
		formcolumndomain.setName(name);
		
		return dynamicFormFacade.genQueryField(formcolumndomain, formMap);
	}
}
